#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    vector<vector<int>> matrixReshape(vector<vector<int>> &mat, int r, int c)
    {
        vector<int> tmp;
        int x = 0;
        for (int i = 0; i < mat.size(); ++i)
        {
            for (int j = 0; j < mat[i].size(); ++j)
            {
                tmp.push_back(mat[i][j]);
                ++x;
            }
        }
        if (x != r * c)
        {
            return mat;
        }
        vector<vector<int>> ret;
        int count = 0;
        for (int i = 0; i < r; ++i)
        {
            vector<int> t;
            for (int j = 0; j < c; ++j)
            {
                t.push_back(tmp[count++]);
            }
            ret.push_back(t);
        }
        return ret;
    }
};